package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.service.ArticleService;
import com.rdgk.newtech.service.ParametersService;
import com.rdgk.newtech.service.ProjectService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.apache.shiro.session.Session;
import org.apache.shiro.SecurityUtils;
import org.slf4j.LoggerFactory;
import com.rdgk.newtech.util.constants.Constants;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: zandaoguang
 * @description: 文章相关Controller
 */
@RestController
@RequestMapping("/parameters")
public class ParametersController {
    final static Logger logger =
            LoggerFactory.getLogger(KeywordController.class);
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ParametersService parametersService;
    @Autowired
    private ProjectService projectService;

    /**
     * 查询文章列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listParameters")
    public JSONObject listParameters(HttpServletRequest request) {
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        return parametersService.listParameters(CommonUtil.request2Json(request));
    }

    @RequiresPermissions("project:list")
    @PostMapping("/getProjectListById")
    public JSONObject getProjectListById(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
//		CommonUtil.hasAllRequired(requestJson, "id,content");
        return projectService.getProjectListById(requestJson);
    }


    /**
     * 搜索文章列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listParametersBySearch")
    public JSONObject listParametersBySearch(HttpServletRequest request) {
        return parametersService.listParametersBySearch(CommonUtil.request2Json(request));
    }


    /**
     * 搜索文章列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listParametersByOrder")
    public JSONObject listParametersByOrder(HttpServletRequest request) {
        return parametersService.listParametersByOrder(CommonUtil.request2Json(request));
    }


    /**
     * 搜索文章列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listParametersByOrderUrl")
    public JSONObject listParametersByOrderUrl(HttpServletRequest request) {
        return parametersService.listParametersByOrderUrl(CommonUtil.request2Json(request));
    }
    @RequiresPermissions("article:list")
    @GetMapping("/listParametersByOrderType")
    public JSONObject listParametersByOrderType(HttpServletRequest request) {
        return parametersService.listParametersByOrderType(CommonUtil.request2Json(request));
    }


    /**
     * 当搜索文章的时候，也就是有搜索内容的时候，排序只能排序搜索内容，不能排序全部
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listArticleByContentHaveSearch")
    public JSONObject listArticleByContentHaveSearch(HttpServletRequest request) {
        return articleService.listArticleByContentHaveSearch(CommonUtil.request2Json(request));
    }


    /**
     * 当搜索文章的时候，也就是有搜索内容的时候，按照创建时间排序只能排序搜索内容，不能排序全部，这一部分是解决带搜索内容的按照创建时间排序的代码
     */
    @RequiresPermissions("article:list")
    @GetMapping("/getListByOrderCreateTimeHaveSearch")
    public JSONObject getListByOrderCreateTimeHaveSearch(HttpServletRequest request) {
        return articleService.getListByOrderCreateTimeHaveSearch(CommonUtil.request2Json(request));
    }

    /**
     * 新增文章
     */
    @RequiresPermissions("article:add")
    @PostMapping("/addParameters")
    public JSONObject addParameters(@RequestBody JSONObject requestJson) {
        CommonUtil.hasAllRequired(requestJson, "source_name,type,url,db_name,local_dir,parallel,time_interval,max_search_pages,last_time_start,last_time_end");
        Session session = SecurityUtils.getSubject().getSession();
        JSONObject userInfo = (JSONObject) session.getAttribute(Constants.SESSION_USER_INFO);
        String username = userInfo.getString("username");
        System.out.println("requestJson:"+requestJson);
        logger.error("用户"+username+"增加一条爬虫参数信息："+requestJson);
        return parametersService.addParameters(requestJson);
    }

    /**
     * 修改文章
     */
    @RequiresPermissions("article:update")
    @PostMapping("/updateParameters")
    public JSONObject updateParameters(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "id,source_name,type,url,db_name,local_dir,parallel,time_interval,max_search_pages,last_time_start,last_time_end");
        Session session = SecurityUtils.getSubject().getSession();
        JSONObject userInfo = (JSONObject) session.getAttribute(Constants.SESSION_USER_INFO);
        String username = userInfo.getString("username");
        System.out.println("requestJson:"+requestJson);
        logger.error("用户"+username+"更新爬虫参数信息为："+requestJson);
        return parametersService.updateParameters(requestJson);
    }


    /**
     * 修改文章
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/deleteParameters")
    public JSONObject deleteParameters(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "id,source_name,is_deleted");
        Session session = SecurityUtils.getSubject().getSession();
        JSONObject userInfo = (JSONObject) session.getAttribute(Constants.SESSION_USER_INFO);
        String username = userInfo.getString("username");
        System.out.println("requestJson:"+requestJson);
        logger.error("用户"+username+"删除一条爬虫参数信息："+requestJson);
        return parametersService.deleteParameters(requestJson);
    }


    /**
     * 根据id删除文章
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/deleteArticleById")
    public JSONObject deleteArticleById(@RequestBody JSONObject requestJson) {
        System.out.println("articleId:"+requestJson);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
//		CommonUtil.hasAllRequired(requestJson, "id,content,deleteStatus");
//		return articleService.deleteArticle(requestJson);
        return new JSONObject();
    }
}
